<template>
  <div class="home_container">
    <!-- 导航栏 -->
    <van-nav-bar class="navTop" fixed>
      <van-button
        class="navTop_searchBtn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 导航栏 -->
    <!-- tab区域 -->
    <van-tabs class="vanTab" v-model="active" swipeable animated>
      <van-tab
        class="vanTabItem"
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 频道文章列表开始 -->
        <article-list :channel="channel"></article-list>
        <!-- 频道文章列表结束 -->
      </van-tab>
      <div slot="nav-right" class="other"></div>
      <div slot="nav-right" class="hamburgerBtn" @click="popupIsShow = true">
        <i class="xcc xccgengduo"></i>
      </div>
    </van-tabs>
    <!-- tab区域 -->
    <!-- 弹出层区域开始 -->
    <van-popup
      v-model="popupIsShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ChannelEdit
        :myChannels="channels"
        :active.sync="active"
        @close-popup="popupIsShow = false"
      ></ChannelEdit>
    </van-popup>
    <!-- 弹出层区域结束 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'home',
  components: {
    ArticleList,
    ChannelEdit
  },
  data() {
    return {
      active: 0,
      channels: [], // 频道列表
      popupIsShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    this.UserloadChannels()
  },
  methods: {
    // 获取tabs列表数据
    async UserloadChannels() {
      try {
        let channels = []
        if (this.user) {
          // 已登录 请求获取用户频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录 判断是否有本地的频道列表数据
          const localChannels = getItem('MY_CHANNELS')
          if (localChannels) {
            // 如果有 拿来使用
            channels = localChannels
          } else {
            // 没有则使用默认列表数据
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道数据失败', err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.home_container {
  padding-top: 174px;
  padding-bottom: 100px;
  .van-nav-bar__title {
    max-width: unset;
  }
  .navTop_searchBtn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    line-height: 64px;
    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }
  /deep/ .vanTab {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .vanTabItem {
      border-right: 1px solid #818283;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }
    .van-tab-active {
      color: #333;
    }
    .van-tabs__line {
      width: 30px !important;
      height: 6px;
      background-color: #3296fa;
      margin-bottom: 8px;
    }
    .hamburgerBtn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.9;
      i.xcc {
        font-size: 33px;
      }
      // &代表寻找父元素
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
    .other {
      flex-shrink: 0; //不参与空间布局flex计算
      width: 66px;
      height: 82px;
    }
  }
}

.article-list {
  position: relative;
  top: 0;
}
</style>
